<!--
 * @Description: 
 * @Author: mmzhang
 * @Date: 2022-07-13 15:10:11
 * @LastEditTime: 2023-01-05 16:04:32
-->
<template>
  <Modal v-model="modal" :title="title" :closable="false" :transfer="true"
    :mask-closable="false" :width='1200' :footer-hide="true">
    <dt-search ref="search" v-model="searchInfo" :items="searchOpt" @search="onSearch"
      @reset="onReset" :xl="5" />
    <div class="flex flex-wrap flex-horizontal-between">
      <dt-filter ref='filter' v-model="filterInfo" @filter='onFilter' />
      <div class="flex btn-content flex-vertical-center " slot="btn">
        <p style="margin-right:8px;">{{infoStr}}</p>
        <Button type="primary" @click="addChecked()" style="margin-right:8px;"
          :disabled="!selected.length">添加已选订单</Button>
        <Button type="primary" @click="addAllFun()" style="margin-right:8px;"
          :disabled="!list.length">添加全部订单</Button>
        <Button type="primary" @click="cancelCheck()" style="margin-right:8px;">取消添加</Button>
      </div>
    </div>
    <dt-grid ref="table" :columns="columns" :list="list" :height="400" :select="select">
    </dt-grid>
    <page :pageData="pageData" @pageOnChangeFun="pageOnChangeFun"
      @pageOnPageSizeChangeFun="pageOnPageSizeChangeFun"></page>
  </Modal>
</template>
<script>
  import searchOpt from './searchCheck'
  import checkSubCol from './checkSubCol'
  import checkConCol from './checkConCol'
  import adjustMgr from '@/biz/sales/adjustOrder'
  import dt from '@dt/dt'
  export default {
    watch: {
      selected: {
        handler(n) {
          if (this.isSub) {
            this.infoStr =
              `已选${this.selected.length}个认购单，共${this.pageData.total}个认购单`
          } else {
            this.infoStr =
              `已选${this.selected.length}个签约单，共${this.pageData.total}个签约单`
          }
        },
        deep: true
      },
      'pageData.total': {
        handler(n) {
          if (this.isSub) {
            this.infoStr =
              `已选${this.selected.length}个认购单，共${this.pageData.total}个认购单`
          } else {
            this.infoStr =
              `已选${this.selected.length}个签约单，共${this.pageData.total}个签约单`
          }
        },
        deep: true
      }
    },
    data() {
      return {
        modal: false,
        title: '请选择并添加需要调整的认购单',
        infoStr: '',
        isSub: false,
        filterParams: {},
        searchInfo: {
          adjustType: this.isSub ? 'SUBSCRIBER' : 'CONTRACTOR',
          housesId: dt.buildInfo().housesId,
          productId: '', //产品
          buildingId: '',
          unitId: '',
          isWeb: true
        },
        searchOpt: [],
        filterInfo: {},
        columns: [],
        pageData: {
          total: 0,
          current: 1,
          rows: 50,
          pageSizeOpts: [50, 40, 30, 20, 10],
        },
        selected: [],
        list: [],
        select: {
          one: (selection, row) => {
            if (selection.some(i => i.orderId == row.orderId)) {
              this.selected.push(row)
            } else {
              let idx = this.selected.findIndex(i => i.orderId == row.orderId)
              if (idx >= 0) {
                this.selected.splice(idx, 1)
              }
            }
          },
          all: (selection) => {
            let tmp = []
            if (selection.length > 0) {
              selection.forEach(item => {
                if (!this.selected.some(i => i.orderId == item.orderId)) {
                  tmp.push(item)
                }
              })
              this.selected = [...this.selected, ...tmp]
            } else {
              this.list.forEach(item => {
                let idx = this.selected.findIndex((i, idx) => i.orderId == item.orderId)
                console.log(idx)
                if (idx >= 0) {
                  this.selected.splice(idx, 1)
                }
              })
            }
          }
        }
      }
    },
    methods: {
      show(isSub) {
        this.searchInfo = {
          adjustType: isSub ? 'SUBSCRIBER' : 'CONTRACTOR',
          housesId: dt.buildInfo().housesId,
          productId: '', //产品
          buildingId: '',
          unitId: '',
          isWeb: true
        }
        this.isSub = isSub
        if (this.isSub) {
          this.title = "请选择并添加需要调整的认购单"
          this.columns = new checkSubCol(this).options()
        } else {
          this.title = '请选择并添加需要调整的签约单'
          this.columns = new checkConCol(this).options()
        }

        this.searchOpt = new searchOpt(this).options()
        this.list = []
        this.getList()
        this.modal = true
      },
      hide() {
        this.pageData.current = 1
        this.pageData.rows = 50
        this.selected = []
        this.$refs.filter.clear()
        this.$refs.search.clear()
        this.$emit('onSearch')
        this.modal = false
      },
      onFilter(info) {
        this.selected = []
        this.pageData.current = 1
        this.getList()
      },
      getList() {
        this.$refs.table.loading = true
        let data = {
          page: this.pageData.current,
          rows: this.pageData.rows,
          ...this.filterInfo,
          ...this.searchInfo
        }
        if(!data.adjustType||!data.housesId){return false}
        this.filterParams = { ...data }
        adjustMgr.getCanAdjustmentOrder(data).then(res => {
          this.list = res.list
          this.pageData.total = Number(res.total)
          this.findSelected()
          this.$refs.table.loading = false
        }).catch(err => {
          this.$refs.table.loading = false
        })
      },
      findSelected() {
        let tmp = []
        this.list.forEach(item => {
          if (this.selected.some(i => i.orderId == item.orderId)) {
            tmp.push(item)
          }
        })
        this.$refs.table.selectRows(tmp)
      },
      // 搜索
      onSearch(val) {
        this.pageData.current = 1
        this.selected = []
        this.searchInfo = { ...val }
        this.getList()
      },
      // 清空筛选条件
      onReset() {
        this.pageData.current = 1
        this.selected = []
        this.searchInfo = {
          adjustType: this.isSub ? 'SUBSCRIBER' : 'CONTRACTOR',
          housesId: dt.buildInfo().housesId,
          productId: '', //产品
          buildingId: '',
          unitId: '',
          isWeb: true
        }
        this.searchOpt = new searchOpt(this).options()
        this.list = []
        this.getList()
      },
      // 分页 页码改变事件
      pageOnChangeFun(current) {
        this.getList()
      },
      // 分页 每页条数改变事件
      pageOnPageSizeChangeFun(rows) {
        this.getList()
      },
      addAllFun() {
        let otherParams = {
          adjustmentId: this.$route.query.adjustmentId,
          adjustType: this.isSub ? 'SUBSCRIBER' : 'CONTRACTOR'
        }
        let params = { ...this.filterParams, ...otherParams }
        adjustMgr.addAllOrderFun(params).then(res => {
          this.$Message.success('添加成功')
          this.hide()
        }).catch(err => {})
      },
      addChecked() {
        let params = {
          adjustmentId: this.$route.query.adjustmentId,
          orderLists: this.selected.map(item => {
            return {
              orderId: item.orderId,
              currentAdjustArea: item.area,
              currentAdjustPrice: this.isSub ? item.sumPrice : item.totalPrice
            }
          })
        }
        adjustMgr.auditAdjustmentOrderFun(params).then(res => {
          this.$Message.success('添加成功')
          this.hide()
        }).catch(err => {})
      },
      cancelCheck() {
        this.hide()
      }
    }
  }
</script>